﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.IO;

public partial class InsertImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            urlForm.Visible = false;
            btnClose.Attributes.Add("onclick", "Cancel_Clicked(); return false;");
        }
        if (rd1.Checked)
        {
            uploadForm.Visible = true;
            urlForm.Visible = false;
        }
        else
        {
            uploadForm.Visible = false;
            urlForm.Visible = true;
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (rd1.Checked)
        {
            UploadImage();
        }
        else
        {
            txtImageURL.Value = txtURL.Text;
            txtHeightUnit.Value = txtHeight.Text;
            txtWidthUnit.Value = txtWidth.Text;
            ClientScript.RegisterStartupScript(this.GetType(), "Info", "<script>alert('Ảnh đã được chèn')</script>");
        }
    }

    private void UploadImage()
    {
        if (RadUploadI1.UploadedFiles.Count > 0)
        {
            foreach (UploadedFile validFile in RadUploadI1.UploadedFiles)
            {
                string targetFolder = Server.MapPath("Images/Temp");
                validFile.SaveAs(Path.Combine(targetFolder, validFile.GetName()), true);
                txtImageURL.Value = "Images/Temp/" + validFile.GetName();
                txtHeightUnit.Value = txtHeight.Text;
                txtWidthUnit.Value = txtWidth.Text;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "Info", "<script>alert('Ảnh đã được chèn')</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Info", "<script>alert('Chưa có file nào hoặc định dạng file không chính xác')</script>");
        }
    }
}
